require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var vm = new Vue({
    el:"#myOrderInfo",
    data:{
        num:"",
        uid:"",
        orderNumber:"",
        preorderList:{},
        address:{},
        isScreen:"",
        state:"",
        imgSrc:"",
        number:"",
        orderInfo:{},
        type:""
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.uid=this.searchObject&&this.searchObject.uid;
        this.num=this.searchObject&&this.searchObject.type;
        this.orderNumber=this.searchObject&&this.searchObject.orderNumber;
        this.userOrderInfo();
    },
    methods: {
        //后退
        getBack: function () {
            history.go(-1);
        },
        bgimg: function (url) {
            url = url ? url : "";
            return {
                "background-image": 'url("' + url + '"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //订单详情
        userOrderInfo:function(){
            var that=this;
            var data={
                loginUid:that.uid,
                orderNumber:that.orderNumber
            };
            that.ajax({url:"/purchase/userOrderInfo",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.orderInfo=b.json.data;
                    that.address=that.orderInfo.address;
                    console.log(that.orderInfo)

                }else{
                    vm.$refs.prompt.toast("获取订单详情失败！");
                }
            });
        },
        compareDate:function(item){
            var that=this;
            // console.log(item)
            var second;
            if(item){
                second=getCompareDate(item.currentTime,item.limitTime,"second");
            }else{
                if(that.orderInfo.currentTime){
                    second=getCompareDate(that.orderInfo.currentTime,that.orderInfo.limitTime,"second");
                    console.log(second)
                }
            }
            if(second<0){
                that.type=1;
            }
            return second;
        },
        //时分秒倒计时
        leftTimer:function(item,ins){
            var that=this;
            that.$nextTick(function(){
                if(item){
                    var second=getCompareDate(item.currentTime,item.limitTime,"second");
                    addTimer("data"+ins,second);
                }else{
                    if(that.orderInfo.currentTime){
                        var second1=getCompareDate(that.orderInfo.currentTime,that.orderInfo.limitTime,"second");
                        addTimer("footerData",second1);
                    }
                }

            });
        },
        getIsScreen:function (num,list) {
            console.log(list)
            this.isScreen=num;
            if(num==1&&list){
                this.imgSrc=list.imgSrc;
                this.number=list.number;
                this.state=list.isStorage;
                this.cartIdid=list.cartId;
            }else if(num==2){

            }
        },
        //是否加入储物箱
        getCartIsStorage:function(){
            var that=this;
            var data={
                loginUid:that.uid,
                cartId:that.cartIdid
            };
            if(this.state==0){
                data.state=1;
            }else{
                data.state=0;
            }
            that.ajax({url:"/purchase/cartIsStorage",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.orderInfo.subList.forEach(function(key){
                        if(that.cartIdid==key.cartId){
                            if(key.isStorage==0){
                                key.isStorage=1;
                            }else{
                                key.isStorage=0;
                            }
                        }
                    });
                    that.getIsScreen();
                    vm.$refs.prompt.toast("加入储物箱成功！");
                }else{
                    vm.$refs.prompt.toast("加入储物箱失败！");
                }
            });
        },
        //支付
        payOrder:function(){
            var that=this;
            if(!that.orderInfo&&that.orderInfo.payMoney>0){
                vm.$refs.prompt.toast("请前往app结算支付！");
                return false;
            }
            var data={
                loginUid:that.uid,
                ordersId:that.orderInfo.orderNumber
            };
            console.log(data)
            that.ajax({url:"/purchase/payOrder",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    vm.$refs.prompt.toast("支付成功！");
                    that.getBack();
                }else{
                    vm.$refs.prompt.toast("结算订单失败！");
                }
            });
        },
        getNext:function(num){
            if(num==1){
                location.href="/purchase/applyRefund?type=1&uid="+this.uid+"&orderNumber="+this.orderNumber;
            }else if(num==2){
                location.href="/purchase/applyRefund?type=2&uid="+this.uid+"&orderNumber="+this.orderNumber;
            }else if(num==3){
                if(this.orderInfo.state==3){
                    vm.$refs.prompt.toast("待商家发货，暂无物流信息！");
                }else{
                    location.href="/happyi/refundInfo?type=3&orderNumber="+this.orderInfo.courierNumber+"&uid="+this.uid+"&courier="+encodeURI(this.orderInfo.courier)
                }
            }else if(num==4){
                location.href="/addAddress?type=2&uid="+this.uid+"&addressId="+this.orderInfo.addressId;
            }
        },
        //删除订单
        deleteuserOrder:function(){
            var that=this;
            var data={
                // loginUid:1,
                loginUid:that.uid,
                orderNumber:that.orderInfo.orderNumber
            };
            this.ajax({url:"/purchase/deleteuserOrder",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.getBack();
                }else{
                    vm.$refs.prompt.toast("获取订单列表失败！");
                }
            });
        },
        //取消订单
        canceluserOrder:function(){
            var that=this;
            var data={
                // loginUid:1,
                loginUid:that.uid,
                orderNumber:that.orderInfo.orderNumber
            };
            this.ajax({url:"/purchase/canceluserOrder",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.userOrderInfo();
                }else{

                    vm.$refs.prompt.toast("取消订单失败！");
                }
            });
        },
        //确认收货
        confirmCollectGoods:function(){
            var that=this;
            var data={
                // loginUid:1,
                loginUid:that.uid,
                orderNumber:that.orderInfo.orderNumber
            };
            this.ajax({url:"/purchase/confirmCollectGoods",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.userOrderInfo();
                }else{
                    vm.$refs.prompt.toast("确认收货！");
                }
            });
        }
    }
    ,watch:{
        orderInfo:function(){
            // this.$nextTick(function(){
            //     var second1=getCompareDate(this.orderInfo.currentTime,this.orderInfo.limitTime,"second");
            //     console.log($("#footerData"))
            //     addTimer("footerData",second1);
            // });
        }
    }
});
var addTimer = function () {
    var list = [],
        interval;

    return function (id, time) {
        if (!interval)
            interval = setInterval(go, 1000);
        list.push({ ele: document.getElementById(id), time: time });
    };
    function go() {
        for (var i = 0; i < list.length; i++) {
            list[i].ele.innerHTML= getTimerString(list[i].time ? list[i].time -= 1 : 0);
            if (!list[i].time)
                list.splice(i--, 1);
        }
    }
    function getTimerString(time) {
        h = Math.floor(time/ 3600);
        m = Math.floor((time% 3600) / 60);
        s = Math.floor((time% 3600) % 60);
        if (time>0)
            return checkTime(h) + ":" + checkTime(m) + ":" + checkTime(s);
        else return "00:00:00";
    }
    function checkTime(i) {
        if (i<10) {
            i = "0" +i;
        }
        return i;
    }
} ();
//比较日前大小
function getCompareDate(startTime, endTime, diffType) {
    console.log(startTime)
    startTime = JSON.stringify(startTime).split("-").join("/");//replace(/-/g, '/')
    endTime = JSON.stringify(endTime).split("-").join("/");
    //将计算间隔类性字符转换为小写
    diffType = diffType.toLowerCase();
    var sTime =new Date(JSON.parse(startTime)); //开始时间
    var eTime =new Date(JSON.parse(endTime)); //结束时间
    //作为除数的数字
    var timeType =1;
    switch (diffType) {
        case"second":
            timeType =1000;
            break;
        case"minute":
            timeType =1000*60;
            break;
        case"hour":
            timeType =1000*3600;
            break;
        case"day":
            timeType =1000*3600*24;
            break;
        default:
            break;
    }
    return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(timeType));
}